﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Web;
using Extensions;

[DataContract]
public struct CartStatus
{
    private int _Count;
    private string _SubTotal;

    [DataMember]
    public int Count
    {
        get
        {
            return _Count;
        }
        set
        {
            _Count = value;
        }
    }
    [DataMember]
    public string SubTotal
    {
        get
        {
            return _SubTotal;
        }
        set
        {
            _SubTotal = value;
        }
    }
}
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class ShoppingCartService
{
    // Add [WebGet] attribute to use HTTP GET
    [OperationContract]
    public CartStatus AddProductToCart(int productId)
    {
        ProfileCommon profile = (ProfileCommon)HttpContext.Current.Profile;
        using (DAL.EShopDBDataContext db = new DAL.EShopDBDataContext())
        {
            var prod = db.Products.Single(c => c.ProductId == productId);
            profile.ShoppingCart.InsertItem(productId, prod.Title, (decimal) prod.Price);
            CartStatus cs = new CartStatus();
            cs.Count = profile.ShoppingCart.Items.Count;
            cs.SubTotal = profile.ShoppingCart.Total.ToMoneyString();
            return cs;
        }
    }
}
